import Vue from 'vue'
import Node from '@/components/Node'
import Line from '@/components/Line'

let startId = 1

function getId () {
  return startId++
}

/**
 * 生成一个node节点
 * @param    {String || Object} dom 选择器或dom节点
 * @param    {Object} options 配置信息
 * @return   {Object} Vue组件
 * @Author   Jiangzhiwei
 * @datetime 2021-10-19T15:18:36+0800
 */
export const createNode = (dom, options) => {
  dom = typeof dom === 'string' ? document.querySelector(dom) : dom
  const container = document.createElement('div')
  container.setAttribute('id', 'node')
  dom.appendChild(container)
  return new Vue({
    render: (h) => {
      return h(
        Node,
        {
          props: Object.assign({}, options, { id: getId() })
        }
      )
    }
  }).$mount('#node')
}

export const createLine = (dom, options) => {
  dom = typeof dom === 'string' ? document.querySelector(dom) : dom
  const container = document.createElement('div')
  container.setAttribute('id', 'line')
  dom.insertBefore(container, dom.childNodes[0])
  return new Vue({
    render: (h) => {
      return h(
        Line,
        {
          props: Object.assign({}, options)
        }
      )
    }
  }).$mount('#line')
}
